<template>
  <div>
    <el-card>
      <el-form>
        <el-form-item label="属性名" prop="tmName">
          <el-input
            class="input"
            size="small"
            placeholder="请输入属性名"
            v-model="attrInfo.attrName"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-plus"
            size="small"
            :disabled="!attrInfo.attrName"
            type="primary"
            @click="addAttr"
            >添加属性值</el-button
          >
          <el-button size="small" @click="$emit('toggle', 0)">取消</el-button>
        </el-form-item>
        <el-table :data="attrValueList" style="width: 100%" border class="mt-bottom">
          <el-table-column type="index" label="序号" width="60" align="center"> </el-table-column>
          <el-table-column label="属性值名称" width="700px">
            <template v-slot="{ row, index }">
              <div style="height: 35px">
                <el-input
                  v-if="row.isEdit"
                  size="small"
                  v-model="row.attrName"
                  ref="input"
                  @blur="inputBlur11(row)"
                />
                <div class="content" v-else @click="edit(row)">{{ row.attrName }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作" width="200px">
            <template v-slot="{ row }">
              <el-button type="danger" @click="deleteTrade(row.attrId)" size="mini" class="button"
                ><i class="el-icon-delete icon"></i
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-form-item>
          <el-button size="small" :disabled="!attrValueList.length" @click="submit" type="primary"
            >保存{{ addOrEdit === 1 ? '添加' : '修改' }}</el-button
          >
          <el-button size="small" @click="$emit('toggle', 0)">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { nanoid } from 'nanoid'

export default {
  name: 'AddOrEdit',
  props: ['addOrEdit', 'id'],
  data() {
    return {
      attrInfo: { attrName: '', categoryLevel: 3 },
      attrValueList: [],
      attrRow: {},
      id1: null
    }
  },
  methods: {
    addAttr() {
      this.attrValueList.push({ isEdit: true, valueName: '', attrId: nanoid() })
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    deleteTrade(attrId) {
      var index = this.attrValueList.findIndex(item => {
        return item.attrId === attrId
      })
      this.attrValueList.splice(index, 1)
    },
    inputBlur11(row) {
      row.isEdit = false
    },
    edit(row) {
      row.isEdit = true
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    async submit() {
      try {
        if (!this.attrInfo.attrName) {
          this.$message.error('属性名称不能为空哦!')
          return
        }
        var bool = this.attrValueList.every(item => {
          return item.attrName
        })
        if (bool) {
          var attrValueList = this.attrValueList.map(item => {
            return {
              valueName: item.attrName,
              attrId: undefined
            }
          })

          var obj = { ...this.attrInfo, categoryId: this.id, attrValueList }
          if (this.addOrEdit === 1) {
            await this.$store.dispatch('attr/addAttr', obj)
            this.$message.success('添加属性成功!!!')
          } else {
            obj.id = this.id1
            await this.$store.dispatch('attr/addAttr', obj)
            this.$message.success('修改属性成功!!!')
          }

          this.$emit('toggle', 0)
        } else {
          this.$message.error('属性值不能为空哦')
        }
      } catch (error) {
        console.log(error)
      }
    }
  },
  mounted() {
    if (this.addOrEdit == 2) {
      const result = this.$store.state.attr.attrRow
      this.attrInfo.attrName = result.attrName
      this.id1 = result.id
      this.attrInfo.categoryId = result.categoryId
      this.attrValueList = result.attrValueList.map(item => {
        return { ...item, attrName: item.valueName }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.input {
  width: 200px;
  height: 30px;
}
.content {
  height: 30px;
}
</style>
